package com.zpark.resume.controller;

import com.zpark.resume.common.ResponseResult;
import com.zpark.resume.common.StatusCode;
import com.zpark.resume.entity.Cv;
import com.zpark.resume.entity.Member;
import com.zpark.resume.entity.vo.CvVO;
import com.zpark.resume.service.ICvService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @PackageName: com.zpark.resume.controller
 * @ClassName: CvController
 * @Description:简历控制器
 * @author: RZS
 * @date: 2023/6/1  16:02
 */
@RestController
@RequestMapping("/cv")
@CrossOrigin
public class CvController {

    @Autowired
    private ICvService cvService;

    /**
     * 搜索简历
     * @param direction
     * @return
     */
    @GetMapping("/search/{direction}")
    public ResponseResult<List<Cv>> searchDirection(@PathVariable("direction") String direction){
        // 调用业务层查询数据
        List<Cv> cvList = cvService.searchCvByDirection(direction);
        return new ResponseResult<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), cvList);
    }

    /**
     * 查询公开的简历
     * @return
     */
    @GetMapping("/search_open")
    public ResponseResult<List<Cv>> searchOpenCv(){
        // 调用业务层接口查询数据
        List<Cv> cvList = cvService.searchOpenCv();
        return new ResponseResult<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), cvList);
    }

    /**
     * 根据id查询cv数据
     * @param id cv_id
     * @return
     */
    @PostMapping("/find_by_id/{id}")
    public ResponseResult<Cv> findCvById(@PathVariable("id") Integer id){
        // 调用业务层接口查询数据
        Cv cv = cvService.findCvById(id);
        return new ResponseResult<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), cv);
    }

    /**
     * 加载会员编辑简历数据页面
     * @param session
     * @return
     */
    @GetMapping("/load_cv_data")
    public ResponseResult<CvVO> findCvByMemberId(HttpSession session){
        // 从session对象当中获取绑定的会员信息
        Member member = (Member) session.getAttribute("member");
        // 调用业务层接口查询数据
        CvVO cvVO = cvService.findByMemberId(member);
        // 返回结果
        return new ResponseResult<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), cvVO);
    }

    /**
     * 修改简历基本数据
     * @param cv
     * @return
     */
    @PostMapping("/update_cv_basic")
    public ResponseResult<Void> changeCvBasicData(@RequestBody Cv cv){
        // 调用业务层接口实现修改数据
        cvService.changeCvById(cv);
        // 返回结果
        return new ResponseResult<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), null);
    }

    /**
     * 修改简历职位期望数据
     * @param cv
     * @return
     */
    @PostMapping("/update_cv_job")
    public ResponseResult<Void> changeCvJobData(@RequestBody Cv cv){
        // 调用业务层接口实现修改数据
        cvService.changeCvById(cv);
        // 返回结果
        return new ResponseResult<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), null);
    }
}
